package com.demo.util.excle;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.Charsets;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

@Slf4j
public class UtilEasyExcle {
    public UtilEasyExcle() {
    }


    //excle导出  第一个执行的方法①
    public static <T> void export(HttpServletResponse response, String fileName, String sheetName, List<T> dataList, Class<T> clazz) throws IOException {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding(Charsets.UTF_8.name());
            fileName = URLEncoder.encode(fileName, Charsets.UTF_8.name());
            log.info("导出文件地址--" + fileName);
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(dataList);
        } catch (Throwable var6) {
            throw var6;
        }
    }

    public static <T> void export2(HttpServletResponse response, String fileName, String sheetName, List<T> dataList, Class<T> clazz) throws IOException {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding(Charsets.UTF_8.name());
            fileName = URLEncoder.encode(fileName, Charsets.UTF_8.name());
            log.info("导出文件地址--" + fileName);
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), clazz)
                    .registerWriteHandler(CellStyleUtil.getHorizontalCellStyleStrategy())
                    .registerWriteHandler(new CustomRowWriteHandler())
                    .sheet(sheetName).doWrite(dataList);
        } catch (Throwable var6) {
            throw var6;
        }
    }

    //excle导出  第二个执行的方法②
    public static <T> void export(HttpServletResponse response, List<T> dataList, Class<T> clazz) throws IOException {
        try {
            export(response, DateUtils.format(new Date(), "yyyyMMddHHmmss"), "XX数据表", dataList, clazz);
        } catch (Throwable var4) {
            throw var4;
        }
    }

    //excle导入  第一步执行的方法【返回字符缓冲流对象】
    public static <T> ExcelReaderBuilder getReaderBuilder(MultipartFile excel, ReadListener<T> readListener, Class<T> clazz) throws Exception {
        String filename = excel.getOriginalFilename();
        if (StringUtils.isEmpty(filename)) {
            throw new Exception("请上传文件!");
        } else if (!StringUtils.endsWithIgnoreCase(filename, ".xls") && !StringUtils.endsWithIgnoreCase(filename, ".xlsx")) {
            throw new Exception("请上传正确的excel文件!");
        } else {
            try {
                InputStream inputStream = new BufferedInputStream(excel.getInputStream());
                log.info("输出步骤-----01");
                return EasyExcel.read(inputStream, clazz, readListener);
            } catch (IOException var6) {
                var6.printStackTrace();
                return null;
            }
        }
    }

    //监听字符字符缓冲流
    public static <T> List<T> read(MultipartFile excel, int sheetNo, int headRowNumber, Class<T> clazz) throws Exception {
        DataListener<T> dataListener = new DataListener();
        ExcelReaderBuilder builder = getReaderBuilder(excel, dataListener, clazz);
        if (builder == null) {
            return null;
        } else {
            ((ExcelReaderSheetBuilder) builder.sheet(sheetNo).headRowNumber(headRowNumber)).doRead();
            log.info("输出步骤-----02");
            return dataListener.getDataList();
        }
    }

    //保存字符缓冲流的数据
    public static <T> void save(MultipartFile excel, ExcelImporter<T> importer, Class<T> clazz) throws Exception {
        ImportListener<T> importListener = new ImportListener(importer);
        ExcelReaderBuilder builder = getReaderBuilder(excel, importListener, clazz);
        if (builder != null) {
            log.info("输出步骤-----03");
            builder.doReadAll();
        }
    }

    public static <T> List<T> read(MultipartFile excel, Class<T> clazz) throws Exception {
        DataListener<T> dataListener = new DataListener();
        ExcelReaderBuilder builder = getReaderBuilder(excel, dataListener, clazz);
        if (builder == null) {
            return null;
        } else {
            builder.doReadAll();
            log.info("输出步骤-----04");
            return dataListener.getDataList();
        }
    }

    public static <T> List<T> read(MultipartFile excel, int sheetNo, Class<T> clazz) throws Exception {
        log.info("输出步骤-----05");
        return read(excel, sheetNo, 1, clazz);
    }


    //excle导出根据模板导出
    public static <T> void export5(HttpServletResponse response, String fileName, String templateFileName, List<T> dataList) throws IOException {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding(Charsets.UTF_8.name());
            fileName = URLEncoder.encode(fileName, Charsets.UTF_8.name());
            log.info("导出文件地址--" + fileName);
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream()).withTemplate(templateFileName).sheet().doFill(dataList);
            //EasyExcel.write(response.getOutputStream()).withTemplate(templateFileName).sheet().doWrite(dataList);
            //EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(dataList);
        } catch (Throwable var6) {
            throw var6;
        }
    }
}
